import type { HasId } from "@/types/common";
import type { IPeriodsData, IRentMachineOrderDetailVO, IRentMachineOrderInfoList, IRentUpdateDateDTO } from "@/types/rentMachineManage/rentMachineOrderInfo";
import { getRentMobileOrderByIdInfo } from "@/utils/api/rentMobileOrder";

/* 修改订单生成日期 */
function useUpdateDate(toggleState: (status: boolean) => void, fn?: () => void) {
  const updateDateVisible = ref(false);
  const updateDateForm = ref<IRentUpdateDateDTO>({
    id: 0,
    deduction_data: "",
    issue: ""
  });
  /* 不同模式下，所对应的期数列表 */
  const selectPeriodsList = ref<IPeriodsData[]>([]);

  /* 打开生成日期模态框 */
  function handleDeductionData(row: IRentMachineOrderInfoList) {
    updateDateForm.value.id = row.id;
    toggleState(true);
    getRentMobileOrderByIdInfo<IRentMachineOrderDetailVO, HasId>({ id: row.id })
      .then(res => {
        updateDateVisible.value = true;
        updateDateForm.value.deduction_data = res.result.deduction_data;
        selectPeriodsList.value = res.result.bills.map(item => {
          return {
            id: item.id,
            dictLabel: item.title,
            dictValue: item.issue + ""
          };
        });
      })
      .catch(() => {})
      .finally(() => {
        toggleState(false);
      });
  }

  /* 修改生成日期成功回调 */
  function handleDeductionDataSuccess() {
    updateDateVisible.value = false;
    if (fn) {
      fn();
    }
  }

  return {
    updateDateVisible,
    updateDateForm,
    selectPeriodsList,
    handleDeductionData,
    handleDeductionDataSuccess
  };
}

export default useUpdateDate;
